using UnityEngine;
using System.Collections.Generic;

public class BulletPool : Singleton<BulletPool>
{
    public GameObject BulletPrefab;

    public Queue<GameObject> _queue;

    public int InitNumber = 20;

    private void Start()
    {
        GameObject bullet;

        _queue = new Queue<GameObject>();
        for (int i = 0; i < InitNumber; i++)
        {
            bullet = Instantiate(BulletPrefab, transform);
            bullet.SetActive(false);
            _queue.Enqueue(bullet);
        }

    }
    /// <summary>
    /// 获得子弹
    /// </summary>
    /// <returns></returns>
    public GameObject GetBullet()
    {
        GameObject b;
        if (_queue.Count > 0)
        {
            b = _queue.Dequeue();
            b.SetActive(true);
        }
        else
        {
            b = Instantiate(BulletPrefab, transform);
        }
        return b;
    }
    /// <summary>
    /// 回收子弹
    /// </summary>
    /// <param name="b"></param>
    public void RecoverBullet(GameObject b)
    {
        b.SetActive(false);
        _queue.Enqueue(b);
    }

}
